.PHONY: build run test clean docker-build docker-run deps lint fmt vet help

# 应用信息
APP_NAME := pi-mapper
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")

# Go 相关变量
GO_VERSION := 1.20
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)

# 目录
BIN_DIR := bin
LOG_DIR := logs
CONFIG_DIR := configs

# 构建参数
LDFLAGS := -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME) -X main.gitCommit=$(GIT_COMMIT)"

# 默认目标
.DEFAULT_GOAL := help

##@ 开发相关

help: ## 显示帮助信息
	@echo "PiMapper 构建工具"
	@echo "版本: $(VERSION)"
	@echo ""
	@echo "使用方法:"
	@echo "  make [target]"
	@echo ""
	@echo "可用目标:"
	@awk 'BEGIN {FS = ":.*##"; printf "\n"} /^[a-zA-Z_-]+:.*?##/ { printf "  %-15s %s\n", $$1, $$2 } /^##@/ { printf "\n%s\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

deps: ## 安装依赖
	@echo "安装依赖..."
	go mod download
	go mod tidy

fmt: ## 格式化代码
	@echo "格式化代码..."
	go fmt ./...

lint: ## 运行代码检查
	@echo "运行代码检查..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run; \
	else \
		echo "请先安装 golangci-lint: https://golangci-lint.run/usage/install/"; \
	fi

vet: ## 运行 go vet
	@echo "运行 go vet..."
	go vet ./...

test: ## 运行测试
	@echo "运行测试..."
	go test -v ./...

test-coverage: ## 运行测试并生成覆盖率报告
	@echo "运行测试并生成覆盖率报告..."
	go test -v -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html

##@ 构建相关

build: deps fmt vet ## 构建应用
	@echo "构建应用..."
	@mkdir -p $(BIN_DIR)
	go build $(LDFLAGS) -o $(BIN_DIR)/$(APP_NAME) ./cmd/server

build-linux: deps fmt vet ## 构建 Linux 版本
	@echo "构建 Linux 版本..."
	@mkdir -p $(BIN_DIR)
	GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BIN_DIR)/$(APP_NAME)-linux-amd64 ./cmd/server

build-arm: deps fmt vet ## 构建 ARM 版本 (树莓派)
	@echo "构建 ARM 版本 (树莓派)..."
	@mkdir -p $(BIN_DIR)
	GOOS=linux GOARCH=arm go build $(LDFLAGS) -o $(BIN_DIR)/$(APP_NAME)-arm ./cmd/server

build-arm64: deps fmt vet ## 构建 ARM64 版本
	@echo "构建 ARM64 版本..."
	@mkdir -p $(BIN_DIR)
	GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o $(BIN_DIR)/$(APP_NAME)-arm64 ./cmd/server

build-windows: deps fmt vet ## 构建 Windows 版本
	@echo "构建 Windows 版本..."
	@mkdir -p $(BIN_DIR)
	GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o $(BIN_DIR)/$(APP_NAME)-windows-amd64.exe ./cmd/server

build-all: build-linux build-arm build-arm64 build-windows ## 构建所有平台版本

##@ 运行相关

run: build ## 构建并运行应用
	@echo "运行应用..."
	./$(BIN_DIR)/$(APP_NAME)

run-dev: ## 开发模式运行
	@echo "开发模式运行..."
	go run ./cmd/server

##@ Docker 相关

docker-build: ## 构建 Docker 镜像
	@echo "构建 Docker 镜像..."
	docker build -t $(APP_NAME):$(VERSION) .
	docker tag $(APP_NAME):$(VERSION) $(APP_NAME):latest

docker-run: ## 运行 Docker 容器
	@echo "运行 Docker 容器..."
	docker run -d \
		--name $(APP_NAME) \
		-p 8080:8080 \
		-v $(shell pwd)/configs:/app/configs \
		-v /mnt/sda:/mnt/sda \
		$(APP_NAME):latest

docker-stop: ## 停止 Docker 容器
	@echo "停止 Docker 容器..."
	docker stop $(APP_NAME) || true
	docker rm $(APP_NAME) || true

##@ 清理相关

clean: ## 清理构建产物
	@echo "清理构建产物..."
	rm -rf $(BIN_DIR)
	rm -rf $(LOG_DIR)
	rm -f coverage.out coverage.html

clean-all: clean ## 完全清理（包括依赖）
	@echo "完全清理..."
	go clean -cache -testcache -modcache

##@ 部署相关

install-deps: ## 安装系统依赖
	@echo "安装系统依赖..."
	# Ubuntu/Debian
	sudo apt-get update && sudo apt-get install -y redis-server
	# CentOS/RHEL
	# sudo yum install -y redis
	# macOS
	# brew install redis

setup-redis: ## 设置 Redis
	@echo "设置 Redis..."
	sudo systemctl start redis-server
	sudo systemctl enable redis-server

setup-directories: ## 设置目录权限
	@echo "设置目录权限..."
	@mkdir -p $(LOG_DIR)
	@mkdir -p $(CONFIG_DIR)
	@sudo mkdir -p /mnt/sda
	@sudo chmod 755 /mnt/sda

##@ 开发工具

watch: ## 文件变更自动重启
	@echo "启动文件监控..."
	@if command -v air >/dev/null 2>&1; then \
		air; \
	else \
		echo "请先安装 air: go install github.com/cosmtrek/air@latest"; \
	fi

swagger: ## 生成 Swagger 文档
	@echo "生成 Swagger 文档..."
	@if command -v swag >/dev/null 2>&1; then \
		swag init -g cmd/server/main.go -o docs; \
	else \
		echo "请先安装 swag: go install github.com/swaggo/swag/cmd/swag@latest"; \
	fi

##@ 版本信息

version: ## 显示版本信息
	@echo "版本信息:"
	@echo "  应用版本: $(VERSION)"
	@echo "  构建时间: $(BUILD_TIME)"
	@echo "  Git 提交: $(GIT_COMMIT)"
	@echo "  Go 版本: $(shell go version)"

##@ 帮助信息

info: ## 显示项目信息
	@echo "PiMapper 项目信息"
	@echo "=================="
	@echo "应用名称: $(APP_NAME)"
	@echo "版本: $(VERSION)"
	@echo "Go 版本: $(GO_VERSION)"
	@echo "目标平台: $(GOOS)/$(GOARCH)"
	@echo "二进制目录: $(BIN_DIR)"
	@echo "日志目录: $(LOG_DIR)"
	@echo "配置目录: $(CONFIG_DIR)"